Grokking Google Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Google Easy
Add Binary (easy)
Solution: Add Binary
Valid Mountain Array (easy)
Solution: Valid Mountain Array
Set Mismatch (easy)
Solution: Set Mismatch
Is Subsequence (easy)
Solution: Is Subsequence
Length of Last Word (easy)
Solution: Length of Last Word
Search Insert Position (easy)
Solution: Search Insert Position
Repeated Substring Pattern (easy)
Solution: Repeated Substring Pattern
Monotonic Array (easy)
Solution: Monotonic Array
Count Nodes Equal to Average of Subtree (easy)
Solution: Count Nodes Equal to Average of Subtree
Find the Difference (easy)
Solution: Find the Difference
How Many Numbers Are Smaller Than the Current Number (easy)
Solution: How Many Numbers Are Smaller Than the Current Number
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Find the Difference of Two Arrays (easy)
Solution: Find the Difference of Two Arrays
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Solution: Repeated Substring Pattern

Problem Statement

Given a string s, return true if it can be formed by taking any substring of it and appending multiple copies of the substring together.

A substring is a contiguous sequence of characters in the string.

Examples

  • Example 1:

    • Input: s = "bcdbcdbcdbcd"
    • Expected Output: true
    • Justification: The input string can be formed by repeating the substring "bcd" three times.
  • Example 2:

    • Input: s = "abcdabcd"
    • Expected Output: true
    • Justification: The input string is the result of the substring "abcd" repeated twice.
  • Example 3:

    • Input: s = "aabaaba"
    • Expected Output: false
    • Justification: There's no single substring that, when repeated, forms the input string.

Solution

To solve this problem, we can utilize the concept of string manipulation and iteration. The intuition behind the solution is that if the string can indeed be constructed by repeating a substring, then this pattern should be identifiable by analyzing the structure of the string itself. Essentially, we'll try to find a smaller, repeating pattern within the string that, when extended, recreates the original string.

The most effective approach involves checking each possible substring length from 1 up to half the length of the string (since a repeating substring would be at most half the length of the original string). For each possible length, we replicate the substring and check if it matches the original string. This method is both straightforward and efficient for identifying repeating patterns within strings.

Step-by-Step Algorithm

  1. Determine the length of the input string (s): Calculate the length of s and store it. This length will be used to limit the possible lengths of the repeating substring.

  2. Iterate over possible substring lengths: Loop from 1 up to half the length of s. The rationale is that a repeating substring can be at most half the length of s, because to repeat, the substring needs to appear at least twice.

  3. Check divisibility: For each length i, check if the total length of s is divisible by i. This step ensures that s can be evenly divided into substrings of length i.

  4. Extract and repeat the substring: If the length is divisible, extract a substring of length i starting from the beginning of s. Repeat this substring enough times so that the concatenated result has the same length as s.

  5. Compare the constructed string with the original: Check if the repeated substring, when concatenated together, equals the original string s. If they match, it means s can indeed be formed by repeating a substring, and you return true.

  6. Continue the iteration: If no match is found, continue the iteration with the next possible length.

  7. Return false: If the loop finishes without finding any repeating substring pattern that can form s, return false.

Algorithm Walkthrough

Let's consider the input: s = "bcdbcdbcdbcd"

  1. Length Calculation:

    • The length of s is 12.
  2. Iteration Over Possible Substring Lengths:

    • The possible lengths to check are 1 to 6 (half of 12).
  3. Length 1 (i=1):

    • Check if 12 is divisible by 1 (yes).
    • Repeat "b" 12 times.
    • The result "bbbbbbbbbbbb" does not match s.
  4. Length 2 (i=2):

    • Check if 12 is divisible by 2 (yes).
    • Repeat "bc" 6 times.
    • The result "bcbcbcbcbcbc" does not match s.
  5. Length 3 (i=3):

    • Check if 12 is divisible by 3 (yes).
    • Repeat "bcd" 4 times.
    • The result "bcdbcdbcdbcd" matches s.
    • Return true because we found that s can be formed by repeating "bcd".

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

  • Time Complexity: in the worst case, because for each substring length up to n/2, we're constructing a new string and comparing it to the original string, each operation taking time.
  • Space Complexity: , where n is the length of the input string. This is because we are creating a new string to compare with the original string.
Repeated Substring Pattern (easy)
Monotonic Array (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis